﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.AcmsControls.ContentControls.FrontEnd;
using Acms.Bll;

namespace Ait.Web.Templates.Calculator
{
    public partial class _default : Acms.AcmsControls.TemplatePage
    {
        protected override void OnInit(EventArgs e)
        {
          //  base.OnInit(e);
            
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            btnSubmit.Click += new EventHandler(btnSubmit_Click);
            
            btnSendEmail.Click += new EventHandler(btnSendEmail_Click);
            if (!IsPostBack)
            {
                DataBind();
            }
        }


        void btnSendEmail_Click(object sender, EventArgs e)
        {
            Validate();
            if (IsValid)
            {
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                
                //user email
                mail.To.Add(new System.Net.Mail.MailAddress(emailTxt.Text));
                mail.Bcc.Add("acmsemail@gmail.com");
                mail.From = new System.Net.Mail.MailAddress("customer.services@ait-pg.co.uk", "AIT-PG");
                mail.Subject = "Data Centre Energy Savings Calculator Results";
                mail.Body = string.Concat(
                    "These are the results from your data-centre Carbon Calculator that could be made if the recommendations of an AIT EU Code of Conduct Audit are implemented:", " \r\n ", " \r\n ",
                    "Inefficient: ", inefficientLbl.Text, " \r\n ",
                    "Average: ", averageLbl.Text , " \r\n ",
                    "Efficient: ", efficientLbl.Text, " \r\n "," \r\n ",
                    "Potential Saving : ", savingLbl.Text ," \r\n "," \r\n ",
                    "Many of the recommendations of our Audits are low cost, high impact and easy to adopt.", " \r\n ", " \r\n ",
                    "An AIT data centre audit delivers a detailed report based on the EU Code of Conduct for Best Practice in Data Centres - with graphical representations of key findings based on a full physical inspection of server rooms, non-intrusive temperature readings and an assessment of the needs of users, IT and facilities teams.", " \r\n ", " \r\n ",
                    "Code of Conduct for Best Practice in Data Centres.", " \r\n ",
                    "AIT is recognised as an ‘Endorser' for the European Code of Conduct on Data Centres. This means we:", " \r\n ",
                    "- Provide energy efficiency products & services to data centre owners, operators or consultants", " \r\n ",
                    "- Calculate and present energy efficiency reports", " \r\n ",
                    "- Promote the Code of Conduct best practices", " \r\n ","\r\n ",
                    "Contact us for more information: 0845 017 7017, customer.services@ait-pg.co.uk"
                    );
                try
                {
                    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                    client.Send(mail);
                    mail.Dispose();
                    createMsgPnl.Visible = false;
                   MessageSentPnl.Visible = true;
                }
                catch
                {
                    createMsgPnl.Visible = false;
                    MessageErrorPnl.Visible = true;
                }

                //company email
                System.Net.Mail.MailMessage mailCompany = new System.Net.Mail.MailMessage();
                mailCompany.To.Add("customer.services@ait-pg.co.uk");
                mailCompany.Bcc.Add("acmsemail@gmail.com");
                mailCompany.From = new System.Net.Mail.MailAddress("no-reply@ait-pg.co.uk", "AIT-PG Website");
                mailCompany.Subject = "Urgent Web Enquiry from the Data Centre Energy Savings Calculator Results";
                mailCompany.Body = string.Concat(
                    "The following web enquiry requests contact from AIT Data Centre efficiency team:", " \r\n ", " \r\n ",
                    "Name: ", nameTxt.Text, " \r\n ",
                    "Company: ", copmanyTxt.Text, " \r\n ",
                    "Phone: ", phoneTxt.Text, " \r\n ", " \r\n ",
                    "Email: ", emailTxt.Text, " \r\n ", " \r\n ", " \r\n ",


                    "Please contact (within 1 business day) to discuss how he can save costs by implementing the results of an AIT Data Centre Audit:", " \r\n ", " \r\n ",
                    "kWh Energy Cost: ", kwhCostTxt.Text, " \r\n ",
                    "Rack Density: ", rackDensityDdl.SelectedValue, " \r\n ",
                    "Number of Racks: ", numberOfRackTxt.Text, " \r\n ", " \r\n ", " \r\n ",

                    "Resulting in the following PUE calculations:", " \r\n ", " \r\n ",                
                    "Inefficient (PUE=3) £", inefficientLbl.Text, " \r\n ",
                    "Average (PUE=2): £", averageLbl.Text, " \r\n ",
                    "Efficient (PUE=1.5)£", efficientLbl.Text, " \r\n ", " \r\n ",
                    "Potential Saving : ", savingLbl.Text, " \r\n ", " \r\n ",
                    
                    ""
                    );
                try
                {
                    System.Net.Mail.SmtpClient clientCompany = new System.Net.Mail.SmtpClient();
                    clientCompany.Send(mailCompany);
                    mailCompany.Dispose();
                    createMsgPnl.Visible = false;
                    MessageSentPnl.Visible = true;
                }
                catch
                {
                    createMsgPnl.Visible = false;
                    MessageErrorPnl.Visible = true;
                }
            }
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {

            double inefficientValue =(double.Parse(kwhCostTxt.Text) * ((double.Parse(rackDensityDdl.SelectedValue) * double.Parse(numberOfRackTxt.Text)) * 8760) * 3)/100;
            double averageValue = (double.Parse(kwhCostTxt.Text) * ((double.Parse(rackDensityDdl.SelectedValue) * double.Parse(numberOfRackTxt.Text)) * 8760) * 2)/100;
            double efficientValue = (double.Parse(kwhCostTxt.Text) * ((double.Parse(rackDensityDdl.SelectedValue) * double.Parse(numberOfRackTxt.Text)) * 8760) * 1.5)/100;
            //string.Format("{0:C}", inefficentValue)

            //string inefficientValueTxt=


            inefficientLbl.Text = String.Format("Inefficient: {0:C}", inefficientValue); //inefficientValueTxt;// Convert.ToString(inefficientValue.ToString("C", "en-US"));// val   Convert.ToString(inefficientValue);
            averageLbl.Text = String.Format("Average: {0:C}", averageValue);
            efficientLbl.Text = String.Format("Efficient: {0:C}", efficientValue);

            savingLbl.Text = String.Format("{0:C}", inefficientValue - efficientValue);
            resultsPnl.Visible = true;
           
            createMsgPnl.Visible = true;

        }
        
    }
}
